package com.qfedu.fmmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.fmmall.entity.Product;
import com.qfedu.fmmall.entity.ProductSku;
import com.qfedu.fmmall.entity.ShoppingCart;
import com.qfedu.fmmall.entity.ShoppingCartVO;
import com.qfedu.fmmall.mapper.ProductMapper;
import com.qfedu.fmmall.mapper.ProductSkuMapper;
import com.qfedu.fmmall.mapper.ShoppingCartMapper;
import com.qfedu.fmmall.service.ShoppingCartService;
import com.qfedu.fmmall.vo.ResultVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.*;

/**
 * <p>
 * 购物车  服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
 @Resource
  private ShoppingCartMapper shoppingCartMapper;
    /**
     * 添加商品到购物车接口
     * @param shoppingCart
     * @return
     */
    @Transactional
    @Override
    public ResultVO addShoppingCart(ShoppingCart shoppingCart) {
        try {
            if (shoppingCart == null) {
                return ResultVO.error("购物车不能为空！");
            }
            int i = shoppingCartMapper.insert(shoppingCart);
            if (i > 0) {
                return ResultVO.success(shoppingCart, "200");
            }

        } catch (Exception e) {
           e.printStackTrace();
        }
        return ResultVO.error("添加失败，遇到问题！");
    }

    /**
     * 查询购物车商品接口
     * @param userId
     * @return
     */
    @Override
    public ResultVO selectShoppingCart(Integer userId){
        List<ShoppingCartVO> shoppingCartVO = shoppingCartMapper.selectShoppingCart(userId);

        if (shoppingCartVO == null) {
            return ResultVO.error("购物车为空！");
        }
        return ResultVO.success(shoppingCartVO,"200");
    }

    /**
     * 添加数量商品接口
     * @param cartid
     * @param cartnum
     * @return
     */
    @Override
    @Transactional
    public ResultVO add(Integer cartid, String cartnum) {
        int i=0;
        try {
            ShoppingCart shoppingCart = shoppingCartMapper.selectById(cartid);
            if (shoppingCart == null) {
                return ResultVO.error("没有该信息");
            }
            if ("1".equals(cartnum)){
                  return ResultVO.error("商品数量至少为1!");
            }
            shoppingCart.setCartNum(cartnum);
             i = shoppingCartMapper.updateById(shoppingCart);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return i>0? ResultVO.success(null,"200") : ResultVO.error("遇到问题！修改失败！");

    }

    /**
     *      "查询购物车记录接口", tags = "查询购物车记录接口")
     * @param cids
     * @return
     */
    @Override
    public ResultVO selectScart(String cids) {
         String[]ids = cids.split(",");
        List<Integer> listIds =new ArrayList<>();
         for (String id: ids) {
             listIds.add(Integer.parseInt(id));
         }
        List<ShoppingCartVO> list = shoppingCartMapper.selectScart(listIds);
        return ResultVO.success(list,"200");
    }


}
